package com.mtiiot.fsmanage.controller;


import com.mtiiot.fsmanage.server.RedisServer;
import com.mtiiot.fscore.bean.MetadataInfo;
import com.mtiiot.fscore.inter1.RedisService1;
import com.mtiiot.fscore.utils.StringUtil;
import com.mtiiot.fsmanage.utils.FsViewConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


/**
 * redis服务
 * Created by lcy on 16-8-18.
 */


@Controller
public class RedisController {
    @Autowired
    private RedisServer redisServer;

    @Autowired
    private RedisService1 redisService1;

    @Autowired
    private FsViewConfig fsViewConfig;


    /**
     * 查询redis元数据
     * @return
     */
    @RequestMapping(value="/redis/redisQuery.action",produces="application/json;charset=UTF-8")
    @ResponseBody
    public Object redisQuery(String sysName,String l1Name,String l2Name,String l3Name,String times,String queryKeyWord,int count,String sort)throws Exception{
        Object obj = redisServer.redisQuery(sysName ,l1Name ,l2Name ,l3Name ,times ,queryKeyWord ,count ,sort);
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = mapper.writeValueAsString(obj);
        return jsonStr;
    }

    /**
     * 统计redis元数据
     */
    @RequestMapping(value="/redis/redisStatistics.action",produces="application/json;charset=UTF-8")
    @ResponseBody
    public Object redisStatistics(String sysName,String l1Name,String l2Name,String l3Name,String times) throws Exception {
        Object obj=redisServer.redisStatistics(sysName, l1Name, l2Name, l3Name, times);
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr=mapper.writeValueAsString(obj);
        System.out.println(jsonStr);
        return jsonStr;
    }

    /**
     * 统计redis元数据
     */
    @RequestMapping(value="/redis/redisValueQuery.action",produces="application/json;charset=UTF-8")
    @ResponseBody
    public Object redisValueQuery(String redisKey,String redisType) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Map<String,String> map=null;
        if("string".equals(redisType)){
            String value=redisService1.getCache(new MetadataInfo(fsViewConfig.getFsviewSysId()),redisKey);
            map=new HashMap<String, String>();
            if(StringUtil.isEmpty(value)){
                map.put("value","null");
            }else{
                map.put("value",value);
            }
            String jsonStr=mapper.writeValueAsString(map);
            return jsonStr;
        }
        if("list".equals(redisType)){
            //redisService1.l
            Long len = redisService1.llen(new MetadataInfo(fsViewConfig.getFsviewSysId()),redisKey);
            if (len > 0) {
                List<String> list = redisService1.lrange(new MetadataInfo(fsViewConfig.getFsviewSysId()),redisKey,0,len);
                List l=new ArrayList();
                int row=1;
                for(String v:list){
                    map=new HashMap<String, String>();
                    map.put("row",row+"");
                    map.put("value",v);
                    row++;
                    l.add(map);
                }
                String jsonStr=mapper.writeValueAsString(l);
                return jsonStr;
            }else{
                map=new HashMap<String, String>();
                map.put("value","null");
                String jsonStr=mapper.writeValueAsString(map);
                return jsonStr;
            }
        }
        if("hash".equals(redisType)){
            Map<String,String> mapTmp=redisService1.hgetAll(new MetadataInfo(fsViewConfig.getFsviewSysId()),redisKey);
            if(mapTmp==null||mapTmp.size()==0){
                map=new HashMap<String, String>();
                map.put("value","null");
                String jsonStr=mapper.writeValueAsString(map);
                return jsonStr;
            }else{
                List l=new ArrayList();
                int row=1;
                Set<String> set=mapTmp.keySet();
                for(String key:set){
                    map=new HashMap<String, String>();
                    map.put("row",row+"");
                    map.put("key",key);
                    map.put("value",mapTmp.get(key));
                    row++;
                    l.add(map);
                }
                String jsonStr=mapper.writeValueAsString(l);
                return jsonStr;
            }
        }
        return null;
    }

    /**
     * 修改string类型的redis数据
     */
    @RequestMapping(value="/redis/redisValueUpdateByStringType.action",produces="application/text;charset=UTF-8")
    @ResponseBody
    public Object redisValueUpdateByStringType(String redisKey,String redisValue) throws Exception {
        redisService1.saveOrUpdateCache(new MetadataInfo(fsViewConfig.getFsviewSysId()),redisKey,redisValue);
        return "success";
    }
}
